#include "printerdialog.h"
#include "ui_printerdialog.h"
#include <QPushButton>
#include <QPrinter>
#include <QFileDialog>
#include <QTextEdit>
#include <QPrintPreviewDialog>
#include <QPainter>
#include <QMargins>
#include <QMessageBox>
#include <QWebEngineSettings>
#include <QWebChannel>
#include <QWebEnginePage>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QHBoxLayout>
#include <QTextDocument>
PrinterDialog::PrinterDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PrinterDialog)
{
    ui->setupUi(this);
    //qDebug()<< printer.width();   //793像素
    //qDebug()<< printer.height();  //1123像素
    int width = 793;//this->geometry().width();
    int height = 1123;//this->geometry().height();
    this->setFixedSize(height+50,width); //设置窗体固定大小


    ui->buttonBox->button(QDialogButtonBox::Ok)->setText("预览");//将buttonbox中的ok 变成汉化
    htmlView = new QWebEngineView(this);
    htmlView->load(QUrl("qrc:/file/demo.html"));
//    htmlView->page()->settings()->setAttribute(QWebEngineSettings::ShowScrollBars, false);
//    htmlView->page()->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
//    htmlView->page()->settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, true);
    htmlView->showMaximized();
//    QWebChannel *channel = new QWebChannel(this);
//    channel->registerObject("bridge", (QObject*)bridge::instance());
//    htmlView->page()->setWebChannel(channel);

    htmlView->show();
    ui->gridLayout->addWidget(htmlView);
}

PrinterDialog::~PrinterDialog()
{
    delete ui;
}

void PrinterDialog::accept()
{
    // 构建 Json 对象 - Page
    QJsonObject pageObject;
    pageObject.insert("successReturnNum", "11");

    // 构建 Json 对象
    QJsonObject json;
    json.insert("orderReceive", "QJsonValue(pageObject)");

    // 构建 Json 文档
    QJsonDocument document;
    document.setObject(json);
    QByteArray byteArray = document.toJson(QJsonDocument::Compact);
    QString strJson(byteArray);
    qDebug() << strJson;

    QString aaa = "setData(\'"+strJson+"\')";
    qDebug()<<aaa;
    htmlView->page()->runJavaScript(aaa);
    //    QPrinter printer(QPrinter::ScreenResolution);
    //    printer.setPageSize(QPrinter::A4);
    //    QPrintPreviewDialog *preview = new QPrintPreviewDialog(&printer, htmlView);
    //    preview->resize(1200,1200);
    //    connect(preview, SIGNAL(paintRequested(QPrinter*)),this, SLOT(printPreviewSlot(QPrinter*)));//
    //    //窗口增加最大最小化按钮
    //    Qt::WindowFlags flags=Qt::Dialog;
    //    flags |=Qt::WindowMinMaxButtonsHint;
    //    preview->setWindowFlags(flags);
    //        preview->exec();
    //    delete preview;

    //    QPrinter printer;
    //    QPrintPreviewDialog *previewDialog = new QPrintPreviewDialog(&printer,this);
    //    connect(previewDialog,SIGNAL(paintRequested(QPrinter*)),this,SLOT(printPreviewSlot(QPrinter*) ) );
    //    previewDialog->exec();

    QPrinter printer(QPrinter::HighResolution);
    printer.setOrientation(QPrinter::Landscape);
    //自定义纸张大小
    printer.setPageSize(QPageSize(QPageSize::A4));
    printer.setPaperSize(QSizeF(htmlView->rect().height(), htmlView->rect().width()),QPrinter::Point);
    QPrintPreviewDialog preview(&printer, this);
    preview.setMinimumSize(600,800);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreviewSlot(QPrinter*)));
    preview.exec ();
}

void PrinterDialog::printPreviewSlot(QPrinter *printer)
{
    QWebEngineCallback<bool> callback;
        QPageLayout layout;
        QPageSize pagesize = QPageSize(QPageSize::A4);
        QMarginsF margin = QMarginsF(5,5,5,5);
        layout.setPageSize(pagesize,margin);
        layout.setOrientation(QPageLayout::Landscape);

        QString filePath = "aaa.pdf";
        htmlView->page()->printToPdf(filePath,layout);
    //    htmlView->page()->url();
    //    QMessageBox::information(this,"提示",htmlView->page()->url().toString());
    //   htmlView->page()->print(printer,callback);
    //    ui->textEdit->print(printer);

    printer->setOrientation(QPrinter::Landscape);
    //获取界面的图片
    QPixmap pixmap = QPixmap::grabWidget(htmlView, htmlView->rect());
    QPainter painterPixmap(htmlView);
    painterPixmap.begin(printer);
    QRect rect = painterPixmap.viewport();
    int x = rect.width() / pixmap.width();
    int y = rect.height() / pixmap.height();
    painterPixmap.scale(x, y);
    painterPixmap.drawPixmap(0, 0, pixmap);
    painterPixmap.end();
}
